package com.yingpai.fitness.dvp_2_0.main.activity.mine;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.main.activity.dynamic.DynamicDetailActivity;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.dynamic.DynamicListBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


public class MyDynamicActivity extends CommonActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private int pageNum = 1;
    private int pageSize = 5;
    private TokenBean tokenBean;
    private QuickAdapter<DynamicListBean.MapBean.PageInfoBean.ListBean> quickAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.my_dynamic_activity);
    }

    @Override
    protected void initView() {
        setTitle("我的动态");

        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        refreshLayout.setOnRefreshLoadMoreListener(this);

        quickAdapter = new QuickAdapter<DynamicListBean.MapBean.PageInfoBean.ListBean>(this, R.layout.dynamic_followed_list_item) {
            @Override
            protected void convert(BaseAdapterHelper helper, final DynamicListBean.MapBean.PageInfoBean.ListBean item) {
                helper.getView(R.id.dynamic_folowed_item).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("id", item.getId());
                        bundle.putInt("isPraised", item.getIsPraised());
                        startActivity(DynamicDetailActivity.class, bundle);
                    }
                });

                CircleImageView dynamic_follow_user_head_iv = helper.getView(R.id.dynamic_followed_user_head_iv);
                Glide.with(context)
                        .load(item.getPublisherPhotoUrl())
                        .error(R.mipmap.icon_user_default)
                        .into(dynamic_follow_user_head_iv);

                helper.setText(R.id.dynamic_followed_user_name, item.getPublisherName() != null ? item.getPublisherName() : "");

                helper.setText(R.id.dynamic_follow_date_tv, item.getPublishTime() != null ? item.getPublishTime() + "" : "");

                helper.setText(R.id.follow_brief_tv_content, item.getThemeTitle() != null ? item.getThemeTitle() : "简介内容为空");

                ImageView follow_brief_iv_content = helper.getView(R.id.follow_brief_iv_content);
                Glide.with(context)
                        .load(item.getPhotoUrl())
                        .error(R.mipmap.default_big_bg)
                        .into(follow_brief_iv_content);

                helper.setText(R.id.followed_user_location, item.getPublishLocale() != null ? item.getPublishLocale() : "位置内容为空");

                helper.setText(R.id.followed_user_collect_tv, item.getPraiseNo() != 0 ? item.getPraiseNo() + "" : "0");

                ImageView follow_user_like_iv = helper.getView(R.id.follow_user_like_iv);
                if (item.getIsPraised() == 0) {
                    follow_user_like_iv.setImageResource(R.mipmap.bbs_like_no);
                } else {
                    follow_user_like_iv.setImageResource(R.mipmap.bbs_like);
                }
            }
        };

        listview.setAdapter(quickAdapter);
    }

    @Override
    protected void initData() {
        pageNum = 1;
        requestData();
    }

    private void requestData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("customerId", tokenBean.getId());
        params.put("pageNum", pageNum++);
        params.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.MY_DYNAMIC, params, Url.MY_DYNAMIC, new Callback<DynamicListBean>() {
            @Override
            public void onSuccess(DynamicListBean dynamicListBean) {
                if (dynamicListBean.getResult().equals(Constant.REQUEST_SUCCESS)&&dynamicListBean.getMap()!=null) {

                    for (int i = 0; i < dynamicListBean.getMap().getPageInfo().getList().size(); i++) {
                        dynamicListBean.getMap().getPageInfo().getList().get(i).setPublisherPhotoUrl(dynamicListBean.getMap().getCustomer().getPhotoUrl());
                    }
                    if (pageNum - 1 == 1) {
                        if(dynamicListBean.getMap().getPageInfo().getTotal()>0){
                            empty.setVisibility(View.GONE);
                            quickAdapter.clear();
                            quickAdapter.addAll(dynamicListBean.getMap().getPageInfo().getList());
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        quickAdapter.addAll(dynamicListBean.getMap().getPageInfo().getList());
                    }
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    quickAdapter.notifyDataSetChanged();

                    int pagecount;
                    if (dynamicListBean.getMap().getPageInfo().getTotal() % pageSize == 0) {
                        pagecount = dynamicListBean.getMap().getPageInfo().getTotal() / pageSize;
                    } else {
                        pagecount = dynamicListBean.getMap().getPageInfo().getTotal() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.setNoMoreData(false);
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(dynamicListBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {

        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        requestData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        requestData();
    }
}
